//
//  AELAddDrivingTestController.swift
//  ActualExaminationLine
//
//  Created by iOS-Czz on 2023/10/30.
//

import UIKit
import WMZDialog
class AELAddDrivingTestController: AELBaseViewController {
    @IBOutlet var optionsTF: UITextField!
    
    @IBOutlet var answerTF: UITextField!
    
    @IBOutlet var optionsView: UIView!
    
    @IBOutlet var answerTio: NSLayoutConstraint!
    
    @IBOutlet var typeTF: UITextField!
    @IBOutlet var nameTF: UITextField!
    var type = 1
    override func viewDidLoad() {
        super.viewDidLoad()
        self.typeTF.text = "Multiple choice question"
        self.changePageUI()
        // Do any additional setup after loading the view.
    }
   
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.wx_navigationBar.isHidden = true
    }
    @IBAction func clickBackAction(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    @IBAction func clickSaveAction() {
        self.view.endEditing(true)
       
        if self.nameTF.text!.isEmpty {
            UIView.showTipsView("Please enter title")
            return
        }
        if self.type == 1{
            if self.optionsTF.text!.isEmpty {
                UIView.showTipsView("Please enter options")
                return
            }
        }
        if self.answerTF.text!.isEmpty {
            UIView.showTipsView("Please enter answer")
            return
        }
        
        
        let model = AELDrivingTestModel()
        model.type = self.type
        model.question = self.nameTF.text!
        model.options = self.optionsTF.text ?? ""
        model.answer = self.answerTF.text!
        model.date = Date().toString(format: "yyyy.MM.dd")
        model.isDefault = false
        model.saveToDB()
        UIView.showResultView("New title addition succeed", true)
        let helper = AELDrivingTestModel.getUsingLKDBHelper()
        helper.delete(with: AELDrivingTestModel.self, where: "isDefault = true")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popToRootViewController(animated: true)
        }
    }
    @IBAction func tapChooseTopicTypeAction(_ sender: Any) {
        let dialog = Dialog()
        dialog
        .wTypeSet()(DialogTypeSheet)
        .wDataSet()(["True or false questions","Multiple choice question"])
        .wCancelTitleSet()("Cancel")
        .wEventFinishSet()({ [self](anyID:Any?,path:IndexPath?,type:DialogType) in
            self.typeTF.text = anyID as? String
            self.type = path!.row
            changePageUI()
        })
        _ = dialog.wStart()
    }
    func changePageUI(){
        if self.type == 0{
            self.optionsView.isHidden = true
            self.answerTio.constant = 12
        }else{
            self.optionsView.isHidden = false
            self.answerTio.constant = 74
        }
    }
   
}
